$(function(){
	//对页面中的元素进行抽取.
	//方法太凌乱,统一管理
	//按钮在JS统一进行监听
	var instituteDatagrid,instituteEditBtn,instituteDialog,instituteForm,instituteTb,dialogBtn;
	instituteDatagrid = $("#institute_datagrid");
	instituteEditBtn = $("#institute_editBtn");
	instituteDialog = $("#institute_dialog");
	instituteForm = $("#institute_form");
	instituteTb = $("#institute_tb");
	dialogBtn = $("#dialog_bottom");
	//数据表格
	instituteDatagrid.datagrid({
		fit:true,
		rownumbers:true,
		singleSelect:true,
		pagination:true,
		url:'/institute/list',
		fitColumns:true,
		toolbar:instituteTb,
		columns:[
			[
				{field:'number',align:'center',width:10,title:'学院编号'},
                {field:'name',align:'center',width:10,title:'学院名称'}
			]
		]
	});
	//对话框
	instituteDialog.dialog({
		width:250,
		height:250,
		buttons:dialogBtn,
		closed:true
	});
	//对按钮进行统一事件监听
	$("a[data-cmd]").on("click",function(){
		var cmd = $(this).data("cmd");
		if(cmd){
			cmdObj[cmd]();
		}
	});
	//方法统一管理起来]
	var cmdObj = {
		reload:function(){
			//刷新数据表格
			instituteDatagrid.datagrid("reload");
		},
		cancel:function(){
			instituteDialog.dialog("close");
		},
		add: function () {
			//1.清空表单数据
			instituteForm.form("clear");
			//2.设置对话框的标题
			instituteDialog.dialog("setTitle", "新增");
			//3.打开对话框
			instituteDialog.dialog("open");
		},
		edit: function () {
			var rowData = instituteDatagrid.datagrid("getSelected");
			if (rowData) {
				//1.清空表单数据
				instituteForm.form("clear");
				//2.设置对话框的标题
				instituteDialog.dialog("setTitle", "编辑");
				//3.打开对话框
				instituteDialog.dialog("open");
				//特殊数据的处理
				instituteForm.form("load", rowData);//基于同名匹配规则
			} else {
				$.messager.alert("温馨提示", "请选择一条需要修改的数据", "warning");
			}
		},
		save:function(){
			var url;
			var idVal = $("[name='id']").val();
			if(idVal){
				url = "/institute/update";
			}else{
				url = "/institute/save";
			}
			instituteForm.form("submit",{
				url:url,
				success:function(data){
					data = $.parseJSON(data);
					if(data.success){
						//提示消息,当点确定的时候,关闭对话框,刷新数据表格
						$.messager.alert("温馨提示",data.msg,"info",function(){
							instituteDialog.dialog("close");
							instituteDatagrid.datagrid("reload");
						});
					}else{
						$.messager.alert("温馨提示",data.msg,"error");
					}
				}
			});
		}
	}
})
